#!/bin/bash

set -e

root=$(cd $(dirname ${0})/..; pwd)
build=${root}/build
node_modules=${root}/node_modules
src=${root}/src
third_party=${root}/third_party
tools=${root}/tools

identifier=onnx
virtualenv=${third_party}/virtualenv/${identifier}

if [ $(which python3) ]; then
    python="python3"
else
    python="python"
fi

bold() {
    echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)" 
}

git_sync() {
    mkdir -p "${third_party}"
    if [ -d "${third_party}/${1}" ]; then
        git -C "${third_party}/${1}" fetch --quiet -p
        git -C "${third_party}/${1}" reset --quiet --hard origin/master
    else
        git -C "${third_party}" clone --quiet --recursive ${2}
    fi
    git -C "${third_party}" submodule update --quiet --init
}

clean() {
    bold "onnx clean"
    rm -rf ${virtualenv}
    rm -rf ${third_party}/${identifier}
    rm -rf ${third_party}/onnxmltools
}

sync() {
    bold "onnx sync"
    git_sync onnx https://github.com/onnx/onnx.git
    git_sync onnxmltools https://github.com/onnx/onnxmltools.git
}

install() {
    bold "onnx install"
    case "$(uname)" in
        "Linux")
            [ -n "$(which cmake)" ] || sudo apt install -y cmake
            [ -n "$(which protoc)" ] || sudo apt install -y protobuf-compiler libprotoc-dev
            ;;
        "Darwin")
            brew list cmake > /dev/null 2>&1 || brew install cmake > /dev/null
            brew list protobuf > /dev/null 2>&1 || brew install protobuf > /dev/null
            ;;
    esac
    [ -n "$(python3 -m pip list --format columns --disable-pip-version-check | grep -w virtualenv)" ] || ${python} -m pip install --force-reinstall --user --quiet virtualenv
    [ -d "${virtualenv}" ] || virtualenv --quiet -p ${python} ${virtualenv}
    source ${virtualenv}/bin/activate
    ${python} -m pip install --quiet protobuf
    export ONNX_ML=1
    export ONNX_NAMESPACE=onnx
    ${python} -m pip install --quiet ${third_party}/onnx
    deactivate
}

schema() {
    bold "onnx schema"
    source ${virtualenv}/bin/activate
    ${node_modules}/protobufjs/bin/pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --no-convert --no-verify --no-create --keep-case --decode-text -r onnx -o ${src}/onnx-proto.js ${third_party}/${identifier}/onnx/onnx-ml.proto ${third_party}/${identifier}/onnx/onnx-operators-ml.proto
    node ${tools}/update_pbjs.js array ${src}/onnx-proto.js float_data float 1
    node ${tools}/update_pbjs.js array ${src}/onnx-proto.js double_data double 1
    deactivate
}

metadata() {
    bold "onnx metadata"
    source ${virtualenv}/bin/activate
    pushd ${tools} > /dev/null
    ${python} onnx-script.py metadata
    popd > /dev/null
    deactivate
}

convert() {
    bold "onnx convert"
    source ${virtualenv}/bin/activate
    ${python} -m pip install --quiet ${third_party}/onnxmltools
    ${python} ${tools}/onnx-script.py convert ${1}
    deactivate
}

infer() {
    bold "onnx infer"
    source ${virtualenv}/bin/activate
    ${python} ${tools}/onnx-script.py infer ${1}
    deactivate
}

optimize() {
    bold "onnx optimize"
    source ${virtualenv}/bin/activate
    ${python} ${tools}/onnx-script.py optimize ${1}
    deactivate
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "sync") sync;;
        "install") install;;
        "schema") schema;;
        "metadata") metadata;;
        "convert") convert ${1} && shift;;
        "infer") infer ${1} && shift;;
        "optimize") optimize ${1} && shift;;
    esac
done
